<?php
	class CompCarrierlease extends AppModel
	{
		var $name = "CompCarrierlease";
		var $primarykey = 'id';
		
		//计算传入日期和今天之间的天数
		function calcDaysBetweenTwoDate($date)
		{
			$now_date = date("Y-m-d");   //今天的日期
	        $deadline = date("Y-m-d",strtotime($date));   
	        $d1=strtotime($now_date);   
	        $d2=strtotime($deadline);   
	        $days=round(($d2-$d1)/3600/24); 
	        return $days;
		}
		
		//当租赁时间小于62天的时候显示提醒信息,在/app/vendor/中的alert.php调用
		function alertmessage()
		{
			$where = "CompCarrierlease.is_deleted = '0'";
			$deadlines = $this->find("all",array('conditions'=>array("CompCarrierlease.is_deleted='0'"),
													'fields'=>array("CompCarrierlease.id,CompCarrierlease.deadline,CompCarrierlease.hasBeenAlerted,CompCarrierlease.cid")			
												));
			foreach($deadlines as $deadline)
			{
				$carrier_isNeedToAlert = false;
				$carrierlease_deadline = $deadline["CompCarrierlease"]["deadline"];
				$carrierlease_id = $deadline["CompCarrierlease"]["id"];
				$hasBeenAlerted = $deadline["CompCarrierlease"]["hasBeenAlerted"];
				$days = $this->calcDaysBetweenTwoDate($carrierlease_deadline);
				if($days >= 0 && $days <= 62 && $hasBeenAlerted == '0')
				{
						$carrier_isNeedToAlert = true;
						$this->id = $carrierlease_id;
						$this->saveField('hasBeenAlerted', '1');
						
						$cid = $deadline["CompCarrierlease"]["cid"];
						$users = array($this->findCpyOwner($cid),$this->findCpyFirstOwner($cid));
						$this->setAlertMsg($users,$deadline);
						$this->setAlertEmail($users,$deadline);
				}
			}
			
		}
		//访问公司的拥有者
		function findCpyOwner($cid){
			$user = ClassRegistry::init('User')->findByCid($cid);
			return $user?$user:array();
		}
		//访问公司的负责人
		function findCpyFirstOwner($cid){
			$company = ClassRegistry::init('Company')->findById($cid);
			$pid = $company['Company']['pid'];
			$project = ClassRegistry::init('Project')->findById($pid);
			$first_owner_id = $project['Project']['first_owner'];
			$user = ClassRegistry::init('User')->findById($first_owner_id);
			return $user?$user:array();
		}
		//发站内信提醒
		function setAlertMsg($users = array(),$deadline=array()){
			//生成信息条目本体
			$msgdata = array('subject'=>'租赁到期提醒','content'=>"尊敬的客户您好，您在大科园的租赁时间将于{$deadline['CompCarrierlease']['deadline']}截止，特此通知！",'owner_id'=>1,'is_readed'=>0,'has_feedback'=>0,'auto_sys'=>0,'id_deleted'=>0);
			$msg_id = ClassRegistry::init('Msg')->addalertmsg($msgdata);
			//生成接收信息者条目
			foreach ($users as $user){
				if (!empty($user)){
					$rsdata = array('id_one'=>$user['User']['id'],'relationship'=>'message','id_two'=>$msg_id,'is_readed'=>0);
					ClassRegistry::init('User_msg_relationship')->addalertrelationship($rsdata);
				}
			}
		}
		//发送提醒邮件
		function setAlertEmail($users = array(),$deadline){
			$etitle = '租赁到期';
			$edescription = '您在大科园的租赁时间将于'.$deadline['CompCarrierlease']['deadline'].'截止，特此通知！';
			
			App::import('Helper', 'Action');
			$actionf = new ActionHelper();
			foreach ($users as $user){
				if(!empty($user)){
					$path=Router::url(array('controller' => '/', 'action' =>''), true);
					$actionf->ybsend($path."/phpmailer/testemail.php?description=".base64_encode($edescription)."&title=".base64_encode($etitle)."&email=".base64_encode($user['User']['email'])."&pagename=companycontents");//发送邮件
				}
			}
		}
	}
?>